
var fill= d3.scale.category20();

var Negative_Colours = [ "#ff8c8c" , "#dd0000" ];//light red to solid red
var getNegativeSentimentColour = d3.scale.linear().range(Negative_Colours).domain([0.2,1]);

var Positive_Colours = [ "#64ff64" , "#00971c" ];//light green  to solid green
var getPositiveSentimentColour = d3.scale.linear().range(Positive_Colours).domain([0.2,1]);



//Constructor
function TagCloudChart(containerId, containerWidth, containerHeight){
    var self=this;
    self.containerId=containerId;
    self.containerWidth=containerWidth;
    self.containerHeight=containerHeight;

    this.jsonData=[{"text":"SUCKS", "size":"10", "eventGroupId":"1"}, {"text":"WORLDCUP", "size":"20", "eventGroupId":"2"}
        , {"text":"SINGTEL", "size":"20", "eventGroupId":"2"}
        , {"text":"COMPETITOR", "size":"20", "eventGroupId":"2"}
        , {"text":"WATCH", "size":"15", "eventGroupId":"2"}
        , {"text":"PAID", "size":"2", "eventGroupId":"2"}
        , {"text":"MATCHES", "size":"14", "eventGroupId":"2"}
        , {"text":"FREE", "size":"60", "eventGroupId":"2"}
        , {"text":"TOURNAMENT", "size":"2", "eventGroupId":"2"}
        , {"text":"REBATE", "size":"1", "eventGroupId":"2"}
        ];
    
}

TagCloudChart.prototype.reload=function(){
/*  var jsonUrl=CONST_URL_WCF_EVENT_CLOUD;

  jsonUrl=jsonUrl.replace("{startTime}", pStartTime);

  $("#twitter_tag_cloud").mask("Loading...");
  $("#twitter_tag_cloud_table").mask("Loading...");
    d3.json(jsonUrl, function(error, json) {
        if (error) return console.warn(error);
        jsonData = json;

        containerId=container;
        containerWidth=cWidth;
        containerHeight=cHeight;
        startTime=pStartTime;
        renderTagCloud(container, jsonData);

        $("#twitter_tag_cloud").unmask();
        //$("#twitter_tag_cloud_table").unmask();
        renderTagCloudChart("#twitter_tag_cloud_table",jsonData);
  });*/

    var self=this;
    renderTagCloud(self.containerId, self.jsonData, self.containerWidth, self.containerHeight);

}

function renderTagCloud(containerId, jsonData, containerWidth, containerHeight) {
    //Map JSON Field to D3 required fields
    //[{"eventGroupId":2,"sentiment":1,"size":10,"text":"haze"}
    var wordScale=d3.scale.linear().domain([1,60]).range([10,55]).clamp(true);
    d3.layout.cloud()
        .words(jsonData.map(function(d){
            //alert(wordScale(d.NormalizedScore));
            return {text:d.text, size:wordScale(d.size)}
        }))
        //.rotate(function (d) { return ~~(Math.random() * 2) * (-90); }) // (Math.random() * <orientation>) * <from> - <to>         
        .rotate(0)
        .containerName(containerId)
        .size([containerWidth,containerHeight])
        .timeInterval(10)
        .font("verdana")
        .fontSize(function (d) { return d.size  ;})
        .padding(2)
        .on("end", draw)
        .start();
}

function scaleTextSize(originalSize){
    var minRange=0;
    var maxRange=100;
    
    var scaleMin=10;
    var scaleMax=50;

    var scaledSize=(originalSize/maxRange) * maxFontSize;

    return scaledSize;

}

function getSentimentColour(sentiment, originalSize, index)
{
    var choosenColor;

    if(sentiment==null || sentiment==undefined || !sentiment){
        choosenColor="gray";
    }
    else if(sentiment<-0.1){
        choosenColor=getNegativeSentimentColour(Math.abs(sentiment));
        //alert("negative" + sentiment.toString() + " " + choosenColor);
    }
    else if(sentiment>0.1){
        choosenColor=getPositiveSentimentColour(sentiment);
        //alert("pos" + sentiment.toString() + " " + choosenColor);
    }
    else{
        choosenColor="steelblue"
    }

//alert(sentiment + " " + choosenColor);
    return choosenColor;
}


function draw (words, bounds, containerId, containerWidth, containerHeight) {

    var dx=(containerWidth/2)*0.8;
    var dy=(containerHeight/2);

    d3.select(containerId).append("svg")
        .attr("width", containerWidth)
        .attr("height", containerHeight)
        .append("g")
        .attr("transform", "translate(" + dx + "," + dy + ")")
        .selectAll("text")
        .data(words)
        .enter().append("text")
                        .on("click", function (d) {
            //alert(d.text);
            cloudMenuPopup(d.text, d.group);
            //alert(d.group);
        })
        .style("font-size", function (d) { return d.size + "px"; })
        .style("font-family", "lato")
        .style("fill","white")
        .transition().duration(1000)
//        .style("fill", function (d, i) {                     
  //              return getSentimentColour(d.sentiment, d.size,i); })
//        .style("stroke-width","0.5px")
        //.style("stroke","#eaeaea")
        //.style("stroke-opacity","0.5")
        .attr("text-anchor", "left")
        .attr("transform", function (d) {
            return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
        })
        .text(function (d) { return d.text; })
}
